/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013-11-4
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hnas.sys.core.util.UserUtils;
import com.hnas.sys.service.system.ResourceService;
import com.hnas.sys.service.system.data.DTOBackMenu;
import com.hnas.sys.service.system.data.DTOMenuItem;
import com.hnas.sys.service.system.dto.DTOResource;
import com.hnas.sys.service.system.dto.DTOUser;

/**
 * @author 郑兴(zhengxing)
 *
 */
@Controller
@RequestMapping("/backend")
public class BackendIndexController {
	@Autowired
	private ResourceService resourceService;
	//resource.searchOrder
	@Value(value = "#{component['resource.searchOrder']}")
	private String searchOrderUrl;
	@Value(value = "#{component['resource.orderConfirm']}")
	private String orderConfirmUrl;
	@Value(value = "#{component['resource.refundSearch']}")
	private String refundSearchUrl;
	@RequestMapping("/index") 
	public String index(ModelMap model) throws Exception {
//		if(UserUtils.getCurrentUser() == null || StringUtil.isEmpty(UserUtils.getCurrentUser().getMenu())) {
//			throw new NoPermissionException("您不是管理员，不能访问此地址。");
//		}
		DTOUser user = UserUtils.getCurrentUser();
		if (null!=user){
			if (this.resourceService.hasResource(user.getId(), searchOrderUrl)){
				model.put("searchOrderUrl", searchOrderUrl);
			}
			if (this.resourceService.hasResource(user.getId(), orderConfirmUrl)){
				model.put("orderConfirmUrl", orderConfirmUrl);
			}
			if (this.resourceService.hasResource(user.getId(), refundSearchUrl)){
				model.put("refundSearchUrl", refundSearchUrl);
			}
		
			
		}
		return "backend/index";
	}
		@RequestMapping("/showMenu/{id}") 
	public String showMenu(@PathVariable("id") Long id,HttpServletRequest request) throws Exception {
			DTOResource rc = this.resourceService.get(id);
			//设置默认
//			if ( 
			List<DTOBackMenu> menus = (List<DTOBackMenu>) request.getSession(true).getAttribute("backend_menus");
			if (null!=menus){
				for (DTOBackMenu menu:menus){
					if (menu.getPack().getId()==rc.getMenuPackageID()){
						menu.setSelected(true);
					}else{
						menu.setSelected(false);
					}
					for (DTOMenuItem item:menu.getItems()){
						if (item.getResource().getId()==rc.getId()){
							item.setSelected(true);
						}else{
							item.setSelected(false);
						}
					}
				}
			}
			String redirectUrl = rc.getResource();
			return "redirect:"+redirectUrl;
	}
}
